package com.example.mumu.passwordbook.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.example.mumu.passwordbook.AddAccountActivity;
import com.example.mumu.passwordbook.EditAccountActivity;
import com.example.mumu.passwordbook.MainActivity;
import com.example.mumu.passwordbook.R;
import com.example.mumu.passwordbook.dao.AccountDao;
import com.example.mumu.passwordbook.domain.Account;
import static com.example.mumu.passwordbook.common.Constants.*;
import com.zhouyou.recyclerview.adapter.BaseRecyclerViewAdapter;
import com.zhouyou.recyclerview.adapter.HelperRecyclerViewDragAdapter;
import com.zhouyou.recyclerview.adapter.HelperRecyclerViewHolder;
import com.zhouyou.recyclerview.swipemenu.SwipeMenuLayout;

/**
 * <p>描述：具有侧滑菜单的适配器</p>
 * <p>
 * 作者： zhouyou<br>
 * 日期： 2016/11/1 16:24<br>
 * 版本： v2.0<br>
 */
public class SwipeMenuAdapter extends HelperRecyclerViewDragAdapter<Account> {

    private AccountDao accountDao;

    public enum ItemTypeEnum {

        EDIT, ADD;

        public static ItemTypeEnum valueOfAccount(Account account) {
            return account.getUsername() == null ? ADD : EDIT;
        }
    }

    public SwipeMenuAdapter(Context context,AccountDao accountDao) {
        super(context, R.layout.adapter_swipemenu_layout1, R.layout.adapter_swipemenu_layout2);
        this.accountDao = accountDao;
        //初始化item点击时响应
        this.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<Account>() {
            @Override
            public void onItemClick(View view, Account item, int position) {
                switch (ItemTypeEnum.valueOfAccount(item)) {
                    case ADD:
                        addAccount();
                        break;
                    case EDIT:
                        editAccount(item);
                        break;
                }
            }
        });
    }

    @Override
    protected void HelperBindData(HelperRecyclerViewHolder viewHolder, final int position, Account item) {
        final SwipeMenuLayout superSwipeMenuLayout = (SwipeMenuLayout) viewHolder.itemView;
        superSwipeMenuLayout.setSwipeEnable(true);   //设置是否可以侧滑
        ItemTypeEnum itemType = ItemTypeEnum.valueOfAccount(item);
        switch (itemType) {
            case ADD:
                viewHolder.setText(R.id.tv_name, item.getName())
                        .setImageResource(R.id.iv_icon, item.getIcon().getResource())
                        .setImageResource(R.id.iv_opt, R.drawable.ic_add_black)
                        .setOnClickListener(R.id.ib_add, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                addAccount();
                            }
                        })
                        .setOnClickListener(R.id.iv_opt, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(mContext, "show iv_opt", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setOnClickListener(R.id.ib_delete, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Account account = SwipeMenuAdapter.this.getData(position);
                                accountDao.delete(account.getId());
                                SwipeMenuAdapter.this.remove(account);
                                Toast.makeText(mContext,R.string.delete_account_success,Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case EDIT:
                viewHolder.setText(R.id.tv_name, item.getName())
                        .setImageResource(R.id.iv_icon, item.getIcon().getResource())
                        .setImageResource(R.id.iv_opt, R.drawable.ic_eye)
                        .setOnClickListener(R.id.ib_edit, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(mContext, "show ib_edit", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setOnClickListener(R.id.iv_opt, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(mContext, "show iv_opt", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setOnClickListener(R.id.ib_delete, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(mContext, "show ib_delete", Toast.LENGTH_SHORT).show();
                            }
                        });

                /**
                 * 设置可以非滑动触发的开启菜单
                 */
//                viewHolder.getView(image_iv).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        if (superSwipeMenuLayout.isOpen()) {
//                            superSwipeMenuLayout.closeMenu();
//                        } else {
//                            superSwipeMenuLayout.openMenu();
//                        }
//                    }
//                });
                break;
        }
    }




    @Override
    public int checkLayout(Account item, int position) {
        //通过枚举顺序来决定应该用的布局文件
        return ItemTypeEnum.valueOfAccount(item).ordinal();
    }

    private void addAccount(){
        Intent intent = new Intent(mContext, AddAccountActivity.class);
        ((Activity)mContext).startActivityForResult(intent,Code.REQUEST_ACCOUNT_OPERATE);
    }

    private void editAccount(Account item){
        Intent intent = new Intent(mContext, EditAccountActivity.class);
        intent.putExtra("account",item);
        ((Activity)mContext).startActivityForResult(intent,Code.REQUEST_ACCOUNT_OPERATE);
    }
}
